package com.example.community.util;

/**
 * @Author: huang
 * @Date: 2021/9/26 16:15
 * @Description:    点赞要考虑性能,用redis
 * 由于数据存放在Redis里，DAO层比较简单，直接写一个Redis key工具类，在业务层操作key就行
 */
public class RedisKeyUtil {

    private static  final String SPLIT = ":";
    // 帖子和评论统称为实体
    private static final  String PREFIX_ENTITY_LIKE = "like:entity";
    //若A关注了B，则A是B的Follower（粉丝），B是A的Followee（目标）。
    //我关注的实体
    private static final String PREFIX_FOLLOWEE = "followee";
    //某实体的粉丝
    private static final String PREFIX_FOLLOWER = "follower";
    //用户为key
    private static final String PREFIX_USER_LIKE = "like:user";
    //验证码key前缀
    private static final String PREFIX_KAPTCHA = "kaptcha";
    //登录凭证
    private static final String PREFIX_TICKET = "ticket";
    //缓存用户信息key
    private static final String PREFIX_USER = "user";
    private static final String PREFIX_UV = "uv";
    private static final String PREFIX_DAU = "dau";
    //统计帖子分数的key
    private static final String PREFIX_POST = "post";

    //某个实体的赞
    // like:entity:entityType:entityId -> set(userId) 使用集合存储点赞的用户
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }

    //用户的赞
    public static String getUserLikeKey(int userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    //某个用户关注的实体
    // followee:userId:entityType -> zset(entityId,now),按照时间排序
    public static String getFolloweeKey(int userId, int entityType) {
        return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
    }

    //某个实体拥有的粉丝
    //follower:entityType:entityId -> zset(userId,now)
    public static String getFollowerKey(int entityType, int entityId) {
        return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
    }

    //将验证码存在Redis里，通过Key来获取验证码。但由于使用验证码时用户并没有登录，
    //无法获得用户信息，生成一个随机字符串owner和验证码做对应，来确定是哪个客户端。
    //kaptcha:owner -> set(KaptchaKey, 验证码)
    public static String getKaptchaKey(String owner) {
        return PREFIX_KAPTCHA + SPLIT + owner;
    }

    //登录凭证
    public static String getTicket(String ticket) {
        return PREFIX_TICKET + SPLIT + ticket;
    }

    //用户
    public static String getUser(int userId) {
        return PREFIX_USER + SPLIT + userId;
    }

    /**
     * 单日UV
     */
    public static String getUVKey(String date) {
        return PREFIX_UV + SPLIT + date;
    }

    /**
     *  区间UV
     * @param startDate     开始区间
     * @param endDate       结束区间
     * @return
     */
    public static String getUVKey(String startDate, String endDate) {
        return PREFIX_UV + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     *
     * @param date
     * @return      单日活跃用户DAU
     */
    public static String getDAUKey(String date) {
        return PREFIX_DAU + SPLIT + date;
    }

    /**
     *      区间活跃用户DAu
     */
    public static String getDAUKey(String startDate, String endDate) {
        return PREFIX_DAU + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 计算帖子分数
     */
    public static String getPostScoreKey() {
        return PREFIX_POST + SPLIT + "score";
    }

}
